package com.guigu.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.userservice.dto.DetectionReport;
import com.guigu.userservice.dto.Equipment;
import com.guigu.userservice.dto.Order;
import com.guigu.userservice.entity.BusinessmanEquipment;
import com.guigu.userservice.feign.MyOrderFeign;
import com.guigu.userservice.feign.myEquipmentFeign;
import com.guigu.userservice.mapper.BusinessmanEquipmentMapper;
import com.guigu.userservice.service.BusinessmanEquipmentService;
import com.guigu.userservice.service.BusinessmanService;
import com.guigu.userservice.vo.BusinessmanEquipmentVo;
import com.guigu.userservice.vo.UserCollectionEquipmentVoPC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务员设备关联表 服务实现类
 * </p>
 *
 * @author zcy
 * @since 2025-10-14
 */
@Service
public class BusinessmanEquipmentServiceImpl extends ServiceImpl<BusinessmanEquipmentMapper, BusinessmanEquipment> implements BusinessmanEquipmentService {

    @Autowired
    private BusinessmanService businessmanService;

    @Autowired
    private myEquipmentFeign equipmentFeign;
    @Autowired
    private BusinessmanEquipmentMapper businessmanEquipmentMapper;

    @Autowired
    private MyOrderFeign myOrderFeign;

    @Override
    public R saveBusinessmanEquipment(BusinessmanEquipment businessmanEquipment) {
        QueryWrapper<BusinessmanEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_id",businessmanEquipment.getEquipmentId());
        BusinessmanEquipment businessmanEquipmentY=this.getOne(queryWrapper);
        if (businessmanEquipmentY!=null){
           businessmanEquipmentY.setBusinessmanId(businessmanEquipment.getBusinessmanId());
           businessmanEquipmentY.setResponsibilityType(businessmanEquipment.getResponsibilityType());
           businessmanEquipmentY.setUpdateTime(LocalDateTime.now());
           this.updateById(businessmanEquipmentY);
        }else{
            businessmanEquipment.setAssignTime(LocalDateTime.now());
            this.save(businessmanEquipment);
        }
        return R.ok();
    }

    @Override
    public List<Equipment> queryBusinessmanEquipment(Integer operatorId,Integer pageNum, Integer pageSize, BusinessmanEquipmentVo vo) {
        //1.再根据业务员ID查询业务员与设备关联表，拿到所有设备ID
        LambdaQueryWrapper<BusinessmanEquipment> businessmanEquipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        businessmanEquipmentLambdaQueryWrapper.eq(BusinessmanEquipment::getBusinessmanId,operatorId);
        List<BusinessmanEquipment> businessmanEquipments = businessmanEquipmentMapper.selectList(businessmanEquipmentLambdaQueryWrapper);
        //2.再根据设备ID判断Order表，如果订单表有这个设备，那么就展示
        //2.1,定义一个空List，存放设备
        List<Equipment> equipmentList = new ArrayList<>();
        for (BusinessmanEquipment BusinEq:businessmanEquipments) {
            //2.1根据设备ID查询订单，添加vo里面的searchIsDetected条件
            Order order = myOrderFeign.getOrderByEqIdAndInspection(Long.valueOf(BusinEq.getEquipmentId()), vo.getSearchIsDetected());
            //2.2 判断是否有这个订单
            if (order != null){
                //如果存在，拿到订单里面的设备ID去查询设备
                UserCollectionEquipmentVoPC voPC = new UserCollectionEquipmentVoPC();
                if (vo.getSearchEquipmentName()!=null){
                    voPC.setBrand(vo.getSearchEquipmentName());
                }
                if (vo.getEquipmentType()!=null){
                    voPC.setTypeId(vo.getEquipmentType());
                }
                Equipment equipment = equipmentFeign.selectEquipmentAndImageAndType3(Long.valueOf(BusinEq.getEquipmentId()), voPC);
                if (equipment == null){
                    continue;
                }
                if (vo.getSearchReportAuditStatus()!=null){
                    //根据设备ID查询检测报告,再加筛选条件
                    DetectionReport report = equipmentFeign.selectDetectionReportByEqIdAndAudit(Long.valueOf(BusinEq.getEquipmentId()), vo.getSearchReportAuditStatus());
                    if (report == null){
                        continue;
                    }
                }

                equipmentList.add( equipment);

            }
        }


        return  equipmentList;
    }

    @Override
    public R queryBusinessmanEquipment1(Integer operatorId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //在通过businessmanId查询业务员设备关联表
        QueryWrapper<BusinessmanEquipment> wrapperBusinessmanEquipment = new QueryWrapper<>();
        wrapperBusinessmanEquipment.eq("businessman_id",operatorId);
        List<BusinessmanEquipment> businessmanEquipmentList = this.list(wrapperBusinessmanEquipment);
        for (BusinessmanEquipment item : businessmanEquipmentList) {
            //调用feign接口，拿到设备详情     将ID转换成Long
            Equipment equipment = equipmentFeign.selectEquipmentByIdFeign(Long.valueOf(item.getEquipmentId()));
            item.setEquipment(equipment);
        }
        PageInfo<BusinessmanEquipment> info = new PageInfo<>(businessmanEquipmentList);
        return R.ok().data("pageInfo", info);
    }



}
